package starter.game.netty.contxt;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufOutputStream;
import io.netty.buffer.PooledByteBufAllocator;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;

import java.util.Map;

import com.bowlong.bio2.B2Helper;
import com.bowlong.util.NewMap;

@SuppressWarnings("rawtypes")
public class SvcContext extends AbstractChannelContext {
	public SvcContext(ChannelHandlerContext c) throws Exception {
		super(c, 0, new NewMap());
	}

	public SvcContext(ChannelHandlerContext c, int inLen, Map params)
			throws Exception {
		super(c, inLen, params);
	}

	@Override
	public int send(Map params) throws Exception {
		// byte[] data = B2Helper.toBytes(params); // write bio2 data
		// ByteBuf bf = chc.alloc().buffer();
		ByteBuf bf = PooledByteBufAllocator.DEFAULT.buffer();
		ByteBufOutputStream out = new ByteBufOutputStream(bf);
		B2Helper.toBytes(out, params);
		out.flush();
		chc.writeAndFlush(bf);
		return bf.writerIndex();
		// byte[] data = bf.array();
		// bf.clear();
		// bf.release();
		// out.close();
		// return send(data);
	}

	@Override
	public int send(byte[] data) throws Exception {
		ByteBuf buf = Unpooled.wrappedBuffer(data);
		chc.writeAndFlush(buf);
		return data.length;
	}

	@Override
	public void close() {
	}

	@Override
	public int readInt() throws Exception {
		throw new NoSuchMethodException();
	}

	@Override
	public byte[] readBytes(int len) throws Exception {
		throw new NoSuchMethodException();
	}

	@Override
	public NewMap readMap() throws Exception {
		throw new NoSuchMethodException();
	}

}
